package com.songlanyun.modules.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.modules.article.service.ArticleContentService;
import com.songlanyun.modules.article.service.ArticleCourseService;
import com.songlanyun.modules.article.service.ArticleService;
import com.songlanyun.modules.exception.BbsException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.article.dao.ArticleCourseClassifyDao;
import com.songlanyun.modules.article.entity.ArticleCourseClassify;
import com.songlanyun.modules.article.service.ArticleCourseClassifyService;

import javax.annotation.Resource;


@Service("articleCourseClassifyService")
public class ArticleCourseClassifyServiceImpl extends ServiceImpl<ArticleCourseClassifyDao, ArticleCourseClassify> implements ArticleCourseClassifyService {


    @Resource
    private ArticleService articleService;

    @Resource
    private ArticleCourseService articleCourseService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ArticleCourseClassify> page = this.page(
                new Query<ArticleCourseClassify>().getPage(params),
                new LambdaQueryWrapper<ArticleCourseClassify>()
                        .orderByAsc(ArticleCourseClassify::getOrderIndex)
                        .orderByDesc(ArticleCourseClassify::getCreateTime)
        );

        //设置文章数量
        page.getRecords().forEach(c -> {
            c.setArticleNum(articleCourseService.articleNum(c.getId()));
        });

        return new PageUtils(page);
    }

    @Override
    public void add(ArticleCourseClassify articleCourseClassify) {
        verifyName(articleCourseClassify.getCourseName());
        this.baseMapper.insert(articleCourseClassify);
    }

    @Override
    public void edit(ArticleCourseClassify articleCourseClassify) {
        ArticleCourseClassify classify = this.getById(articleCourseClassify.getId(), true);
        if (!classify.getCourseName().equals(articleCourseClassify.getCourseName())) {
            verifyName(articleCourseClassify.getCourseName());
        }
        this.baseMapper.updateById(articleCourseClassify);
    }


    private void verifyName(String name) {
        Integer count = this.baseMapper.selectCount(
                new LambdaQueryWrapper<ArticleCourseClassify>()
                        .eq(ArticleCourseClassify::getCourseName, name)
        );

        if (count > 0) {
            throw new RRException(BbsException.COURSE_CLASSIFY_NAME_UNIQUE);
        }
    }

    @Override
    public ArticleCourseClassify getById(Long id, Boolean isThrow) {
        ArticleCourseClassify classify = this.baseMapper.selectById(id);
        if (classify == null && isThrow) {
            throw new RRException(BbsException.COURSE_CLASSIFY_NOT_EXIST);
        }
        return classify;
    }

    @Override
    public void del(Long id) {
        //如果创建了文章，则不能删除
        if (articleService.ifCreateArticle(id)) {
            throw new RRException(BbsException.COURSE_CLASSIFY_CREATE_ARTICLE);
        }
        this.baseMapper.deleteById(id);
    }

    @Override
    public List<ArticleCourseClassify> appList(Map<String, Object> params) {
        return this.baseMapper.selectList(
                new LambdaQueryWrapper<ArticleCourseClassify>()
                        .orderByAsc(ArticleCourseClassify::getOrderIndex)
                        .orderByDesc(ArticleCourseClassify::getCreateTime)
        );
    }

}